//
//  MXArcChart.m
//  MXPieChart
//
//  Created by 马彦 on 16/11/3.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXArcChart.h"
#import "MXPieChart.h"

#define k_COLOR_STOCK @[[UIColor colorWithRed:1.000 green:0.783 blue:0.371 alpha:1.000], [UIColor colorWithRed:1.000 green:0.562 blue:0.968 alpha:1.000],[UIColor colorWithRed:0.313 green:1.000 blue:0.983 alpha:1.000],[UIColor colorWithRed:0.560 green:1.000 blue:0.276 alpha:1.000],[UIColor colorWithRed:0.239 green:0.651 blue:0.170 alpha:1.000]]

@interface MXArcChart ()

/** 标题 */
@property (nonatomic, strong) UILabel *titleLal;

//环图间隔 单位为π
@property (nonatomic,assign)CGFloat itemsSpace;
//数值和
@property (nonatomic,assign) CGFloat totolCount;

@property (nonatomic,assign) CGFloat redius;

@property (nonatomic, strong) MXPieChart *pieChart;

@property (nonatomic, strong) UIButton *introduceBtn;

@end

@implementation MXArcChart

@synthesize titleColor = _titleColor;

- (void)setColors:(NSArray<UIColor *> *)colors {
    _colors = colors;
    
}

- (void)setNameArray:(NSArray *)nameArray {
    _nameArray = nameArray;
    self.pieChart.colorArr = _colors;
    self.pieChart.descArr = nameArray;
    self.pieChart.valueArr = _valueDataArr;
    [self.pieChart showAnimation];
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.chartOrigin = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds)/2);
        //圆半径
        _redius = (CGRectGetHeight(self.bounds) -60*k_Width_Scale)/4 + 5;
        _lineLong = 60;
        self.backgroundColor = [UIColor whiteColor];

    }
    return self;
}

- (void)setLineLong:(NSUInteger)lineLong {
    _lineLong = lineLong;
    
}

- (void)setValueDataArr:(NSArray *)valueDataArr {
    _valueDataArr = valueDataArr;
    
    [self configBaseData];
    [self setNeedsDisplay];
}

- (void)configBaseData{
    
    _totolCount = 0;
    //调扇形的间距
    _itemsSpace =  (M_PI * 2.0  * 1.0/ 360)/_valueDataArr.count;
    for (id obj in _valueDataArr) {
        
        _totolCount += [obj floatValue];
    }
}

-(void)drawRect:(CGRect)rect{
    
    CGContextRef contex = UIGraphicsGetCurrentContext();
    
    CGFloat lastBegin = 0;
    //指示线头长度
    CGFloat longLen = _redius + CGRectGetHeight(self.bounds) / 7;
    for (NSInteger i = 0; i<_valueDataArr.count; i++) {
        id obj = _valueDataArr[i];
        CGFloat currentSpace = [obj floatValue] / _totolCount * (M_PI * 2 - _itemsSpace * _valueDataArr.count);
        CGFloat midSpace = lastBegin + currentSpace / 2.0;
        
        CGPoint begin = CGPointMake(self.chartOrigin.x + sin(midSpace) * _redius * 1.5, self.chartOrigin.y - cos(midSpace) * _redius * 1.5);
        
        CGPoint endx = CGPointMake(self.chartOrigin.x + sin(midSpace) * longLen, self.chartOrigin.y - cos(midSpace)*longLen);
        
        lastBegin += _itemsSpace + currentSpace;
        
        [self drawLineWithContext:contex andStarPoint:begin andEndPoint:endx andIsDottedLine:NO andColor:self.colors[i]];
        CGPoint secondP = CGPointZero;
        
        CGSize size = [[NSString stringWithFormat:@"%.01f%c",[obj floatValue] / _totolCount * 100,'%'] boundingRectWithSize:CGSizeMake(200, 100) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10*k_Width_Scale]} context:nil].size;
        
        if (midSpace<M_PI) {
            secondP =CGPointMake(endx.x + self.lineLong*k_Width_Scale, endx.y);
            [self drawText:[NSString stringWithFormat:@"%.01f%c",[obj floatValue] / _totolCount * 100,'%'] andContext:contex atPoint:CGPointMake(secondP.x - 30 * k_Width_Scale, secondP.y - size.height - 2) WithColor:self.titleColor andFontSize:10*k_Width_Scale];
            
        }else{
            secondP =CGPointMake(endx.x - self.lineLong*k_Width_Scale, endx.y);
            [self drawText:[NSString stringWithFormat:@"%.01f%c",[obj floatValue] / _totolCount * 100,'%'] andContext:contex atPoint:CGPointMake(secondP.x - size.width + 30 *k_Width_Scale, secondP.y - size.height) WithColor:self.titleColor andFontSize:10*k_Width_Scale];
        }
        [self drawLineWithContext:contex andStarPoint:endx andEndPoint:secondP andIsDottedLine:NO andColor:self.colors[i]];
        [self drawPointWithRedius:3*k_Width_Scale andColor:self.colors[i] andPoint:begin andContext:contex];
    }
    
    /*********圆*********/
    CGFloat lastBeg = -M_PI/2;
    NSInteger  i = 0;
    for (id obj in _valueDataArr) {
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth = CGRectGetWidth(self.bounds) / 8;
        [self.colors[i] set];
        CGFloat cuttentpace = [obj floatValue] / _totolCount * (M_PI * 2 - _itemsSpace * _valueDataArr.count);
        [path addArcWithCenter:self.chartOrigin radius:_redius startAngle:lastBeg  endAngle:lastBeg  + cuttentpace clockwise:YES];
        
        [path stroke];
        lastBeg += (cuttentpace + _itemsSpace);
        i++;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.chartOrigin = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds)/2);
    //圆半径
    _redius = (CGRectGetHeight(self.bounds))/5 + 5;
    CGFloat PieChartY = (self.bounds.size.height - (_redius + CGRectGetWidth(self.bounds) / 8) * 2) * 0.5;
    self.titleLal.frame = CGRectMake(10, 5, self.bounds.size.width - 20, 30);
    self.pieChart.frame = CGRectMake(10, self.bounds.size.height - PieChartY * 0.5- 20, self.bounds.size.width - 20, 20);
    self.introduceBtn.frame = CGRectMake(CGRectGetMaxX(self.pieChart.frame) - 80, 0, 20, 20);
}

/**
 问号按钮点击响应方法
 */
- (void)introduceOnAction {
    
    if ([_delegate respondsToSelector:@selector(ringChart:introduceOnAction:)]) {
        [_delegate ringChart:self introduceOnAction:nil];
    }
}




- (void)setTitleFont:(UIFont *)titleFont {
    _titleFont = titleFont;
    
    self.titleLal.font = _titleFont;
}

- (void)setTitleName:(NSString *)titleName {
    _titleName = titleName;
    self.titleLal.text = _titleName;
}

- (void)setStyle:(TitleLocationStyle)style {
    _style = style;
    switch (style) {
        case TitleLocationStyleLeft:
            self.titleLal.textAlignment = NSTextAlignmentLeft;
            break;
        case TitleLocationStyleCenter:
            self.titleLal.textAlignment = NSTextAlignmentCenter;
            break;
        case TitleLocationStyleRight:
            self.titleLal.textAlignment = NSTextAlignmentRight;
            break;
        default:
            self.titleLal.textAlignment = NSTextAlignmentCenter;
            break;
    }
}

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    self.titleLal.textColor = titleColor;
    self.pieChart.textColor = titleColor;
}

- (UIColor *)titleColor {
    if (!_titleColor) {
        _titleColor = [UIColor blackColor];
    }
    return _titleColor;
}

- (UILabel *)titleLal {
    if (!_titleLal) {
        _titleLal = [[UILabel alloc] init];
        _titleLal.textAlignment = NSTextAlignmentCenter;
        _titleLal.textColor = [UIColor blackColor];
        _titleLal.font = [UIFont systemFontOfSize:15];
        [self addSubview:_titleLal];
    }
    return _titleLal;
}

- (MXPieChart *)pieChart {
    if (!_pieChart) {
        _pieChart = [[MXPieChart alloc] init];
        _pieChart.backgroundColor = [UIColor clearColor];
        [self addSubview:_pieChart];
    }
    return _pieChart;
}

- (UIButton *)introduceBtn {
    if (!_introduceBtn) {
        _introduceBtn = [[UIButton alloc] init];
        [_introduceBtn setBackgroundImage:[UIImage imageNamed:@"问号"] forState:UIControlStateNormal];
        [_introduceBtn setBackgroundImage:[UIImage imageNamed:@"问号"] forState:UIControlStateHighlighted];
        [_introduceBtn addTarget:self action:@selector(introduceOnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.pieChart addSubview:_introduceBtn];
    }
    return _introduceBtn;
}
@end
